package com.ossjk.oa.advisory.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.advisory.entity.Discipline;
import com.ossjk.oa.advisory.service.IDisciplineService;

/**
 * <p>
 * 专业管理 前端控制器
 * </p>
 *
 * @author Jason
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/advisory/discipline")
public class DisciplineController extends BaseController {
	@Autowired
	private IDisciplineService iDisciplineService;

	@RequiresPermissions("00150005")
	@GetMapping("/list")
	public Object list(String name, String sid, Page page) {
		Wrapper wrapper = Condition.create().isWhere(true).ne("d.isdelete", 2).orderBy("crtm");
		if (!CommonUtil.isBlank(name)) {
			wrapper.like("d.name", name);
		}
		if (!CommonUtil.isBlank(sid)) {
			wrapper.eq("d.sid", sid);
		}
		page = iDisciplineService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("001500050000")
	@PostMapping("/insert")
	public Object insert(Discipline discipline) {
		if (iDisciplineService.insert(discipline)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001500050020")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		Map map = new HashMap();
		Discipline discipline = iDisciplineService.selectById(id);
		map.put("Discipline", discipline);

		return responseSuccess(map);
	}

	@RequiresPermissions("001500050020")
	@PutMapping("/update")
	public Object update(Discipline discipline, String[] areaIds) {
		if (iDisciplineService.updateById(discipline)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("001500050005")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iDisciplineService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
